<template>
  <el-dialog v-model="dialogVisible" :title="'查看记录'" @close="dialogVisible = true">
    <ContentWrap>
      <el-table :data="tableData" :stripe="true" :show-overflow-tooltip="true" :loading="loading">
        <el-table-column label="时间" align="center" prop="createTime" :formatter="dateFormatter" width="180" />
        <el-table-column label="分数" prop="score" align="center" />
        <el-table-column label="操作" align="center">
          <template #default="scope">
            <el-button type="primary" link @click="showTongueReport(scope.row)">报告</el-button>
          </template>
        </el-table-column>
      </el-table>
      <Pagination
:total="total" v-model:page="queryParams.pageNo" v-model:limit="queryParams.pageSize"
        @pagination="getList" />
    </ContentWrap>
    <Modal
    v-model:visible="tongueReportVisible"
    title=""
    width="420px"
    :show-close="true"
    :close-on-click-modal="false"
    custom-class="tongue-report-modal"
  >
    <TongueReport v-if="tongueReportVisible" :report-data="tongueReportData" />
  </Modal>
  </el-dialog>
</template>

<script setup lang="ts">
import { ref, toRefs, reactive } from 'vue'
import { dateFormatter } from '@/utils/formatTime'
import { TongueMemberApi } from '@/api/application/tongue/member'
import TongueReport from '@/components/TongueReport/index.vue'
import * as MemberReportApi from '@/api/member/report'
const tongueReportVisible = ref(false)
const tongueReportData = ref({})
const dialogVisible = ref(false)
const state = reactive({
  loading: false,
  tableData: [],
  total: 0,
  queryParams: {
    userId: '',
    pageNo: 1,
    pageSize: 10
  },
})
const { queryParams, loading, tableData, total } = toRefs(state)
const message = useMessage()
const getList = async () => {
  loading.value = true
  const data = await TongueMemberApi.getMemberReport(queryParams.value)
  tableData.value = data.list
  total.value = data.total
  loading.value = false
}
const open = async (data?: any) => {
  dialogVisible.value = true
  queryParams.value.userId = data.id
  getList()
}
const showTongueReport = (row: MemberReportApi.MemberReportVO) => {
  try {
    // 解析JSON字符串
     const data = row.report
    let reportData
    if (data.includes('.pdf')) {
      reportData = data.replace(/^"|"$/g, '')
    } else {
      reportData = JSON.parse(data)
    }
    tongueReportData.value = reportData
    tongueReportVisible.value = true
  } catch (error) {
    console.error('解析报告数据失败:', error)
    message.error('报告数据格式错误')
  }
}
defineExpose({ open })
</script>

<style scoped lang="scss"></style>